import { server } from '../config/server';
import { useToken } from '../utils/token';

export const course = {
    /*
     * 获取推荐课程
     */
    getCourse(lastId) {
        return new Promise((resolve, reject) => {
            let jwt = wx.getStorageSync('loginToken');
            wx.request({
                url: `${server.server_addr}/course/getCourse`,
                method: 'POST',
                header: {
                    'Authorization': jwt,               // 假设使用 JWT 作为授权头
                    'Content-Type': 'application/json'  // 根据实际情况调整
                },
                data: { lastId },
                success: (res) => {
                    console.log('【 Response in request getCourse 】', res);
                    resolve(res.data);
                    useToken(res);
                },
                fail: (err) => {
                    console.log('【 Error in request getCourse 】', err);
                    reject({ success: false, err });
                }
            });
        });
    },
    /*
     * 查看课程详情
     */
    getCourseInfo(courseId) {
        return new Promise((resolve, reject) => {
            let jwt = wx.getStorageSync('loginToken');
            wx.request({
                url: `${server.server_addr}/course/getCourseInfo`,
                method: 'POST',
                header: {
                    'Authorization': jwt,               // 假设使用 JWT 作为授权头
                    'Content-Type': 'application/json'  // 根据实际情况调整
                },
                data: { courseId },
                success: (res) => {
                    console.log('【 Response in request getCourseInfo 】', res);
                    resolve(res.data);
                    useToken(res);
                },
                fail: (err) => {
                    console.log('【 Error in request getCourseInfo 】', err);
                    reject({ success: false, err });
                }
            });
        });
    },
    /*
     * 查看课程详情
     */
    buyCourse(courseId) {
        return new Promise((resolve, reject) => {
            let jwt = wx.getStorageSync('loginToken');
            wx.request({
                url: `${server.server_addr}/course/buyCourse`,
                method: 'POST',
                header: {
                    'Authorization': jwt,               // 假设使用 JWT 作为授权头
                    'Content-Type': 'application/json'  // 根据实际情况调整
                },
                data: { courseId },
                success: (res) => {
                    console.log('【 Response in request buyCourse 】', res);
                    resolve(res.data);
                    useToken(res);
                },
                fail: (err) => {
                    console.log('【 Error in request buyCourse 】', err);
                    reject({ success: false, err });
                }
            });
        });
    },
    /*
     * 查看课程详情
     */
    getJoinCourse() {
        return new Promise((resolve, reject) => {
            let jwt = wx.getStorageSync('loginToken');
            wx.request({
                url: `${server.server_addr}/course/getJoinCourse`,
                method: 'GET',
                header: {
                    'Authorization': jwt,               // 假设使用 JWT 作为授权头
                    'Content-Type': 'application/json'  // 根据实际情况调整
                },
                success: (res) => {
                    console.log('【 Response in request getJoinCourse 】', res);
                    resolve(res.data);
                    useToken(res);
                },
                fail: (err) => {
                    console.log('【 Error in request getJoinCourse 】', err);
                    reject({ success: false, err });
                }
            });
        });
    },
};